package com.ninthgroup.controller;

import com.ninthgroup.entity.User;
import com.ninthgroup.service.UserService;
import com.ninthgroup.vo.Result;
import com.ninthgroup.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ：1452863884@qq.com rcg
 * @date ：Created in 2022/10/13 20:50
 * @description：用户的控制层
 * @modified By：
 * @version:1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * @create by: 1452863884@qq.com rcg
     * @description:查询所有用户
     * 如果使用了@RequestBody 那么请求方式必须为post
     * @create time: 2022/10/15 12:00
     * @param currentPage
     * @param pageSize
     * @param userVo
     * @return com.ninthgroup.vo.Result
     */
    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage,
                       @RequestParam(defaultValue = "5") Integer pageSize,
                       @RequestBody UserVo userVo
    ) {

        return userService.findByCondition(currentPage, pageSize, userVo);
    }

    @GetMapping("/del")
    public Result delete(Integer uId){
        return userService.deleteUserId(uId);
    }

    @PostMapping("/add")
    public Result add(@RequestBody User user){
        return userService.save(user);
    }

    @PostMapping("/update")
    public Result update(@RequestBody User user){
        return userService.update(user);
    }

    @PostMapping("/select")
    public Result select(@RequestBody UserVo userVo){
        return userService.select(userVo.getUsername(),userVo.getRealname());
    }

    @GetMapping("/findOne")
    public Result findOne(Integer uId){
        return userService.findOneById(uId);
    }


}